package user

import (
	"context"
	"github.com/gin-gonic/gin"
	etcd "github.com/rpcxio/rpcx-etcd/client"
	"github.com/smallnest/rpcx/client"
	"h_rpcx/client/basics"
	"h_rpcx/internet/service/BannerService"
	"h_rpcx/internet/service/userService"
)

type User struct {
	userclient client.XClient
}

func NewUser() *User {
	user, err := etcd.NewEtcdV3Discovery(basics.EtcdS.BasePath, basics.EtcdS.UserPath, []string{basics.EtcdS.Addr}, false, nil)
	if err != nil {
		panic(err.Error())
	}
	userclient := client.NewXClient("User", client.Failtry, client.RandomSelect, user, client.DefaultOption)
	return &User{userclient: userclient}
}

func (u *User) UserTest(ctx *gin.Context) {
	us := &userService.Req{Name: "张鑫"}
	resp := &userService.Resp{}
	err := u.userclient.Call(context.Background(), "Add", us, resp)
	if err != nil {
		panic(err.Error())
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"data": resp.Name,
	})
}

func (u *User) Banner(ctx *gin.Context) {
	resp := &BannerService.BannerResp{}
	err := u.userclient.Call(context.Background(), "GetBanner", &BannerService.BannerReq{}, resp)
	if err != nil {
		panic(err.Error())
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"data": resp.Banners,
	})
}
